856A - Set Theory - CodeForces Solution


brute force constructive algorithms *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
//for math u can observe something related to gcd or something related to even or odd to draw some conclusions
//if your answer is lieing in a range then binary search is the best approach to solve that problem
//for errors check out of bound and overflow of integers
//sorting,binary search,dynamic programming,greedy are some of the most used techniques to solve the problems
//until some element it is good and then next it is bad u should use binary search 
//to find the first good element or to find the first bad element we use binary search
//if there are digits then remember that the digits are just from 0-9 so u can use two loops the same for character a-z they are just 26
const int M=998244353;
const int INF=1e9+5;
void dfs(int currentNode,vector<vector<int>>& adj,int parent,vector<int>& ans)
{
    ans.push_back(currentNode);
    for(int neighbour : adj[currentNode])
    {
        if(neighbour!=parent)
        {
            dfs(neighbour,adj,currentNode,ans);
        }
    }
}
ll mod(ll x)
{
    return (((x%M)+M)%M);
}
ll add(ll a,ll b)
{
    return mod(mod(a)+mod(b));
}
ll mul(ll a,ll b)
{
    return mod(mod(a)*mod(b));
}
int sumd(ll a)
{
    int res=0;
    while(a!=0)
    {
        res+=a%10;
        a/=10;
    }
    return res;
}
int main() {
	// your code goes here
	int t;
    cin>>t;
	while(t--)
	{
	  int n;
	  cin>>n;
	  int a[n];
	  for(int i=0;i<n;i++)
	  {
	      cin>>a[i];
	  }
	  if(n==1)
	  {
	      cout<<"YES"<<endl;
	      cout<<1<<endl;
	  }
	  else
	  {
	      cout<<"YES"<<endl;
	   int b[n];
	   b[0]=1;
	   vector<int>diff(1000005,0);
	   for(int i=0;i<n;i++)
	   {
	     int x=abs(a[i]-1);
	     diff[x]=1;
	   }
	   int i=1;
	   int k=2;
	   while(i<n)
	   {
	       bool ok=true;
	       int p=0;
	       while(p==0)
	       {
	         for(int j=0;j<n;j++)
	         {
	             int x=abs(a[j]-k);
	             if(diff[x]==1)
	             {
	                 p=0;
	                 break;
	             }
	             else
	             {
	                 p=1;
	             }
	         }
	         if(p==0) k++;
	         else
	         {
	             b[i]=k;
	             for(int j=0;j<n;j++)
	             {
	                 int x=abs(a[j]-k);
	                 diff[x]=1;
	             }
	             
	             k++;
	             p=1;
	         }
	       }
	       i++;
	   }
	   for(int z=0;z<n;z++)
	   {
	       cout<<b[z]<<" ";
	   }
	   cout<<endl;
	  }
	  
	  }
	

	return 0;

}


Comments

Submit
0 Comments
More Questions

1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls